package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.dto.NameValueSelectDto;
import com.ys.controller.api.sys.ApiInterYsModalSelectorController;
import com.ys.entity.sys.YsModalSelector;
import com.ys.entity.sys.YsModalSelectorField;
import com.ys.enume.sys.YsModalSelectorFieldEnum;
import com.ys.mapper.sys.SysMapper;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.YsModalSelectorFieldService;
import com.ys.util.StringUtil;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/sys/ysModalSelectorField")
public class WebYsModalSelectorFieldController {
	
	@Autowired
	private CommonApiService<YsModalSelectorField> commonApiService;
	
	@Autowired
	private SysMapper sysMapper;
	
	@Autowired
	private YsModalSelectorFieldService ysModalSelectorFieldService;
	
	/*
	 * 批量新增字段
	 */
	@GetMapping("/toBatchAdd")
	public String toFlow(Model model, Long pid, String tableName){
		if (pid == null) {
			return W.error(model, "菜单ID不能为空");
		}
		if (tableName == null) {
			return W.error(model, "表名不能为空");
		}
		
		/*
		 *  字段详情数组
		 *  1、已有的字段
		 *  2、未有的字段
		 */
		List<ToInitColsDto.RowDto> rows = new ArrayList<ToInitColsDto.RowDto>();
		List<YsModalSelectorField> listEntity = new YsModalSelectorField().setPid(pid).queryList();
		List<String> listCode = new ArrayList<String>();
		for(YsModalSelectorField detail : listEntity) {
			
			String code = detail.getCode();
			String name = detail.getName();
			rows.add(new ToInitColsDto.RowDto(pid, code, name));
			listCode.add(code);
		}
		
		List<Map<String, Object>> listDetail = sysMapper.listSysCol(tableName);
		for(Map<String, Object> detail : listDetail) {
			String code = StringUtil.firstCharacterToLower(detail.get("code").toString());
			if (listCode.contains(code)) {
				continue;
			}
			if ("id".equals(code)) {
				continue;
			}
			String name = detail.containsKey("name") ? detail.get("name").toString() : "待完善名称";
			rows.add(new ToInitColsDto.RowDto(pid, code, name));
		}
		
		ToInitColsDto dto = new ToInitColsDto(rows);
		model.addAttribute("sonlist", dto);
		
		return "sys/ysModalSelectorField/toBatchAdd";
	}
	@Data
	@AllArgsConstructor
	public static class ToInitColsDto{
		private List<RowDto> rows; // 

		@Data
		@AllArgsConstructor
		public static class RowDto{
			private Long pid;
			private String code;
			private String name;
		}
	}
	
	/**
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, Long pid) throws Exception{
		
		if (pid == null) {
			return W.error(model, "所属弹出框ID不能为空");
		}
		YsModalSelector ysModalSelector = new YsModalSelector().setId(pid).queryFirst();
		
		// 排序序号
		Integer sortNo = sysMapper.getNextYsModalSelectorFieldOfSortNo(pid);
		
		YsModalSelectorField entity = new YsModalSelectorField();
		// 设置默认值
		entity.setSortNo(sortNo)
			.setPid(pid)
			.setWidth(160)
			.setAlign(YsModalSelectorFieldEnum.Align.CENTER.value)
			.setType(YsModalSelectorFieldEnum.Type.TEXT.value)
			.setStateCondition(YsModalSelectorFieldEnum.StateCondition.NO.value);
		
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("pname", ysModalSelector.getName());
		return commonApiService.toAdd(model, entity, ApiInterYsModalSelectorController.MENU_ID, extraParam, null);
	}
	@Data
	@AllArgsConstructor
	private static class ToAddDto{
		private Long pid; // 
		private Integer sortNo; // 
		private List<NameValueSelectDto> typeArr; // 字段类型
		private List<NameValueSelectDto> stateConditionArr; // 是否查询
	}
	
	/**
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, Long id){
		
		YsModalSelector entity = new YsModalSelector().setId(id).queryFirst();
		String name = entity.getName();
		String nameTable = entity.getNameTable();
		ToEditDto dto = new ToEditDto(id, name, nameTable);

		model.addAttribute("dto", dto);
		return "sys/ysModalSelector/toEdit";
	}
	@Data
	@AllArgsConstructor
	private static class ToEditDto{
		private Long id; // 
		private String name;
		private String nameTable; // 
	}
	
	/**
	 * 配置
	 */
	@GetMapping("/toSetting")
	public String toSetting(Model model, Long id){
		
		YsModalSelector entity = new YsModalSelector().setId(id).queryFirst();
		String name = entity.getName();
		String nameTable = entity.getNameTable();
		
		List<YsModalSelectorField> listYsModalSelectorField = new YsModalSelectorField().setPid(id).queryList();
		
		List<ToSettingDto.RowDto> rows = new ArrayList<ToSettingDto.RowDto>();
		for(YsModalSelectorField ysModalSelectorField : listYsModalSelectorField) {
			Long id2 = ysModalSelectorField.getId();
			String code = ysModalSelectorField.getCode();
			String name2 = ysModalSelectorField.getName();
			String type = ysModalSelectorField.getType();
			Integer stateCondition = ysModalSelectorField.getStateCondition();
			rows.add(new ToSettingDto.RowDto(id2, code, name2, type, stateCondition));
		}

		ToSettingDto dto = new ToSettingDto(id, name, nameTable, rows);
		model.addAttribute("dto", dto);
		return "sys/ysModalSelector/toSetting";
	}
	@Data
	@AllArgsConstructor
	private static class ToSettingDto{
		private Long id; // 
		private String name;
		private String nameTable; // 
		private List<RowDto> rows;
		
		@Data
		@AllArgsConstructor
		private static class RowDto{
			private Long id; // 
			private String code; // 
			private String name;
			private String type; // 
			private Integer stateCondition;
		}
	}
	
	/*
	 * 调整顺序
	 */
	@GetMapping("/toResort")
	public String toResort(Model model, HttpServletRequest request, HttpServletResponse response,
			Long pid){
		
		return ysModalSelectorFieldService.toResort(model, pid);
	}
	
	/**
	 * 选项配置
	 */
	@GetMapping("/toOptionSetting")
	public String toOptionSetting(Model model, Long id){
		return ysModalSelectorFieldService.toOptionSetting(model, id);
	}
}
